SlideShare a Scribd company logo
1 of 37
Class No.21  Data Structures http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 2 D A B C k 3 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],A C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],A C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree Case 1a : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s  left  subtree. Action : change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side http://ecomputernotes.com
Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 0 1 2
Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)
Deletion in AVL Tree Case 1b : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s  right  subtree. Action : (same as  1a )   change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side
Deletion in AVL Tree Case 2a : the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s  left  subtree. Action : change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree. Delete on this side

More Related Content

What's hot

Powering code reuse with context and render props
Powering code reuse with context and render propsPowering code reuse with context and render props
Powering code reuse with context and render propsForbes Lindesay
 
Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)Runcy Oommen
 
Better than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouseBetter than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouseAltinity Ltd
 
Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...Cora Li
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleChristopher Curtin
 
Appendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution modelAppendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution modelCora Li
 
Clustering your Application with Hazelcast
Clustering your Application with HazelcastClustering your Application with Hazelcast
Clustering your Application with HazelcastHazelcast
 
K means clustering
K means clusteringK means clustering
K means clusteringKuppusamy P
 
Application of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPsApplication of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPsNAP Events
 
Discrete control2 converted
Discrete control2 convertedDiscrete control2 converted
Discrete control2 convertedcairo university
 
A Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDAA Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDATomonari Masada
 
Pyclustering tutorial - BANG
Pyclustering tutorial - BANGPyclustering tutorial - BANG
Pyclustering tutorial - BANGAndrei Novikov
 
Guaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTsGuaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTsSun-Li Beatteay
 
The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88Mahmoud Samir Fayed
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programsKaruppaiyaa123
 
BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit Jennifer Stern
 
Project Term2 Computer barun.docx
Project Term2 Computer barun.docxProject Term2 Computer barun.docx
Project Term2 Computer barun.docxSakshamSharma382989
 
Scaling up data science applications
Scaling up data science applicationsScaling up data science applications
Scaling up data science applicationsKexin Xie
 
Elastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programmingElastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programmingSalar Delavar Qashqai
 

What's hot (20)

Powering code reuse with context and render props
Powering code reuse with context and render propsPowering code reuse with context and render props
Powering code reuse with context and render props
 
Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)
 
Better than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouseBetter than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouse
 
Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading example
 
Appendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution modelAppendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution model
 
Clustering your Application with Hazelcast
Clustering your Application with HazelcastClustering your Application with Hazelcast
Clustering your Application with Hazelcast
 
K means clustering
K means clusteringK means clustering
K means clustering
 
Application of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPsApplication of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPs
 
Discrete control2 converted
Discrete control2 convertedDiscrete control2 converted
Discrete control2 converted
 
A Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDAA Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDA
 
Pyclustering tutorial - BANG
Pyclustering tutorial - BANGPyclustering tutorial - BANG
Pyclustering tutorial - BANG
 
Guaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTsGuaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTs
 
The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programs
 
BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit
 
Project Term2 Computer barun.docx
Project Term2 Computer barun.docxProject Term2 Computer barun.docx
Project Term2 Computer barun.docx
 
Scaling up data science applications
Scaling up data science applicationsScaling up data science applications
Scaling up data science applications
 
Control assignment#4
Control assignment#4Control assignment#4
Control assignment#4
 
Elastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programmingElastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programming
 

Viewers also liked

computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38ecomputernotes
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
computer notes - Data Structures - 12
computer notes - Data Structures - 12computer notes - Data Structures - 12
computer notes - Data Structures - 12ecomputernotes
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
computer notes - Data Structures - 18
computer notes - Data Structures - 18computer notes - Data Structures - 18
computer notes - Data Structures - 18ecomputernotes
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1ecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 27
computer notes - Data Structures - 27computer notes - Data Structures - 27
computer notes - Data Structures - 27ecomputernotes
 
computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3ecomputernotes
 
computer notes - Data Structures - 26
computer notes - Data Structures - 26computer notes - Data Structures - 26
computer notes - Data Structures - 26ecomputernotes
 
computer notes - Data Structures - 29
computer notes - Data Structures - 29computer notes - Data Structures - 29
computer notes - Data Structures - 29ecomputernotes
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9ecomputernotes
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7ecomputernotes
 

Viewers also liked (20)

computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
computer notes - Data Structures - 12
computer notes - Data Structures - 12computer notes - Data Structures - 12
computer notes - Data Structures - 12
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
computer notes - Data Structures - 18
computer notes - Data Structures - 18computer notes - Data Structures - 18
computer notes - Data Structures - 18
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 27
computer notes - Data Structures - 27computer notes - Data Structures - 27
computer notes - Data Structures - 27
 
computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
 
computer notes - Data Structures - 26
computer notes - Data Structures - 26computer notes - Data Structures - 26
computer notes - Data Structures - 26
 
computer notes - Data Structures - 29
computer notes - Data Structures - 29computer notes - Data Structures - 29
computer notes - Data Structures - 29
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7
 

More from ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clauseecomputernotes
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Dataecomputernotes
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statementsecomputernotes
 
computer notes - Data Structures - 10
computer notes - Data Structures - 10computer notes - Data Structures - 10
computer notes - Data Structures - 10ecomputernotes
 
Computer notes - Controlling User Access
Computer notes - Controlling User AccessComputer notes - Controlling User Access
Computer notes - Controlling User Accessecomputernotes
 
Computer notes - Using SET Operator
Computer notes - Using SET OperatorComputer notes - Using SET Operator
Computer notes - Using SET Operatorecomputernotes
 

More from ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clause
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Data
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statements
 
computer notes - Data Structures - 10
computer notes - Data Structures - 10computer notes - Data Structures - 10
computer notes - Data Structures - 10
 
Computer notes - Controlling User Access
Computer notes - Controlling User AccessComputer notes - Controlling User Access
Computer notes - Controlling User Access
 
Computer notes - Using SET Operator
Computer notes - Using SET OperatorComputer notes - Using SET Operator
Computer notes - Using SET Operator
 

Recently uploaded

My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

computer notes - Data Structures - 21

  • 1. Class No.21 Data Structures http://ecomputernotes.com
  • 2. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 3. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 4. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 5. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 6. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 7. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 8. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 9. int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
  • 10. int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
  • 11. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 12. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 13. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 14. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 15. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 16. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 17. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 18. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 19. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 2 D A B C k 3 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 20. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 21. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 22. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Deletion in AVL Tree Case 1a : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s left subtree. Action : change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side http://ecomputernotes.com
  • 34. Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 0 1 2
  • 35. Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)
  • 36. Deletion in AVL Tree Case 1b : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s right subtree. Action : (same as 1a ) change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side
  • 37. Deletion in AVL Tree Case 2a : the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s left subtree. Action : change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree. Delete on this side

Editor's Notes

  1. Start of lecture 23
  2. Start of lecture 24
  3. End of lecture 23.